package com.kc.business.service.impl;

import com.kc.business.domain.BizTeacherRelationship;
import com.kc.business.mapper.BizTeacherRelationshipMapper;
import com.kc.business.service.IBizTeacherRelationshipService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BizTeacherRelationshipServiceImpl
 * @Description 师承管理实现类
 * @Author wangdongliang
 * @Date 2021/5/24 16:52
 */
@Service
public class BizTeacherRelationshipServiceImpl implements IBizTeacherRelationshipService {
    @Resource
    private BizTeacherRelationshipMapper teacherRelationshipMapper;
    @Override
    public Integer add(BizTeacherRelationship bizTeacherRelationship) {
        bizTeacherRelationship.setCreateTime(LocalDateTime.now());
        bizTeacherRelationship.setUpdateTime(LocalDateTime.now());
        return teacherRelationshipMapper.insert(bizTeacherRelationship);
    }

    @Override
    public Integer update(BizTeacherRelationship bizTeacherRelationship) {
        bizTeacherRelationship.setUpdateTime(LocalDateTime.now());
        return teacherRelationshipMapper.updateByPrimaryKeySelective(bizTeacherRelationship);
    }

    @Override
    public List<BizTeacherRelationship> getList(BizTeacherRelationship bizTeacherRelationship) {
        List<BizTeacherRelationship> teacherRelationships = teacherRelationshipMapper.getList(bizTeacherRelationship);
        List<BizTeacherRelationship> bizTeacherRelationshipList = new ArrayList<>();
        for (BizTeacherRelationship teacherRelationship : teacherRelationships){
            teacherRelationship.setChildred(getChild(teacherRelationships,teacherRelationship.getId()));
            if(teacherRelationship.getPid().equals(0)){
                bizTeacherRelationshipList.add(teacherRelationship);
            }
        }
        return bizTeacherRelationshipList;
    }

    public List<BizTeacherRelationship> getChild(List<BizTeacherRelationship> bizTeacherRelationships,Integer id){

        List<BizTeacherRelationship> teacherRelationships = bizTeacherRelationships.stream().map(item->{return item;}).filter(item->{return item.getPid().equals(id);}).collect(Collectors.toList());
        return teacherRelationships;
    }

}
